package com.android.weather1;

import android.app.Activity;
//import android.database.Cursor;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
//import android.widget.SimpleCursorAdapter;
import android.util.Log;

//import java.io.IOException;
import java.io.InputStream;

public class Weatherv1 extends Activity {
	
	public static final int INSERT_ID = Menu.FIRST;
	private int mCityNumber = 1;
	private Weatherdbm wDbHelper;
	private static final String classTagSTR = "Weatherdbm";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        StrictMode.ThreadPolicy policy = new StrictMode.
        ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        wDbHelper = new Weatherdbm(this);
        wDbHelper.open();
        fillData();      
       
        getForecast();

    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
        case INSERT_ID:
            createCity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void createCity() {
        String cityName = "City " + mCityNumber++;
        wDbHelper.createCity(cityName, "", "");
        fillData();
    }
    
    private void fillData() {
        /*
    	// Get all of the notes from the database and create the item list
        Cursor c = wDbHelper.fetchAllCities();
        startManagingCursor(c);

        String[] from = new String[] { "name" };
        int[] to = new int[] { R.id.text1 };
        
        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter cities =
            new SimpleCursorAdapter(this, R.layout.city_row, c, from, to);
        //setListAdapter(cities);
        */
    }
    
    private void getForecast() {
        RSSParser saxparser = new RSSParser("http://weather.yahooapis.com/forecastrss?w=2502265");
        WeatherYahoo forecast;
        InputStream is = null;
        /*
        RSSParser saxparser = new RSSParser();
        try {
           is = getAssets().open("weathertest.xml");
        } catch	(IOException e) {
    	   Log.d("XML","onCreate(): parse() failed");
    	   return;
        }
        forecast = saxparser.parse(is);
        */
        forecast = saxparser.parse(is);
      
        Log.d(classTagSTR, "Build date:" + " " + forecast.lastBuildDate);
        Log.d(classTagSTR, forecast.City + " " + forecast.Region + " " + forecast.Country);
        Log.d(classTagSTR, "Condicion:" + " " + forecast.textWeather);
        Log.d(classTagSTR, "Temperature:" + " " + forecast.Temp);
    }

}